import { useUserStore } from "@/store"
import { emitter } from "./mitt"
const whiteList = [ '/pages/userServiceAgreement/index' ]
const navigateTo = (page: string, params?: Object) => {

  if(!useUserStore().isLogined && !whiteList.includes(page)) {
    emitter.emit('toggleLoginPopup', true);
    return;
  }
  uni.navigateTo({
    url: `${page}?params=${JSON.stringify(params)}`,
    fail: (err) => {
      console.log(err);
    }
  })
}

const redirectTo = (page: String, params?: Object) => {
  uni.redirectTo({
    url: `${page}?params=${JSON.stringify(params)}`,
  })
}

const reLaunch = (page: String, params?: Object) => {
  uni.reLaunch({
    url: `${page}?params=${JSON.stringify(params)}`,
  })
}

const navigateBack = (delta: number = 1) => {
  uni.navigateBack({
    delta,
  })
}

export {
  navigateTo,
  redirectTo,
  reLaunch,
  navigateBack,
}
